package com.sghj.config;


import com.sghj.config.handler.ResultCode;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

//统一返回结果
@Data
public class ReturnResult {

    private Boolean success;
    private Integer code;
    private String message;
    private Map<String,Object> data= new HashMap<String,Object>();

    public ReturnResult(Boolean success, Integer code, String message, Map<String, Object> data) {
        this.success = success;
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setData(Map<String, Object> data) {
        this.data = data;
    }

    public Boolean getSuccess() {
        return success;
    }

    public Integer getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public Map<String, Object> getData() {
        return data;
    }

    //私有构造方法，使其无法创建
    private ReturnResult(){};

    //成功静态方法
    public static ReturnResult success(){
        ReturnResult returnResult = new ReturnResult();
        returnResult.setSuccess(true);
        returnResult.setCode(ResultCode.SUCCESS);
        returnResult.setMessage("成功");
        return  returnResult;
    }

    //失败静态方法
    public static ReturnResult failed(){
        ReturnResult returnResult = new ReturnResult();
        returnResult.setSuccess(false);
        returnResult.setCode(ResultCode.FAIL);
        returnResult.setMessage("失败");
        return  returnResult;
    }
    public ReturnResult success(Boolean success){
        this.setSuccess(success);
        return this;
    }

    public ReturnResult message(String message){
        this.setMessage(message);
        return this;
    }

    public ReturnResult code(Integer code){
        this.setCode(code);
        return this;
    }

    public ReturnResult data(String key, Object value){
        this.data.put(key, value);
        return this;
    }

    public ReturnResult data(Map<String, Object> map){
        this.setData(map);
        return this;
    }
}
